/**
 * @Author : woon
 * @Timestamp : 2016-06-29
 */

var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var root = ui("$");
var circleUrl = require("url/circle");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var imgTool = require("helper/picture");
var openHelper = require("helper/open");
var communityUrl = require("url/community");

var createCircleUrl = circleUrl.url.v1_4.createCircle;

//定义ui
var do_ALayout_camera = ui("do_ALayout_camera");

//点击-相机
do_ALayout_camera.on("touch", function(data, e) {
	imgTool.imgCut(selectImgBack);
});

var imgLogo = ui("imgLogo");
function selectImgBack(imgList){
	imgLogo.source=imgList[0];
}
//andriod返回键
page.on("back", function() {
    app.closePage();
});

page.on("loaded", function(){
	circleAdd.init();	
});
/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
ui(addheader+".labelTitle").text = "创建一个圈子";
//左上角-返回
ui(addheader+".headerLeft").on("touch", function() {
    app.closePage()
});

//右上角-发布（提交）
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "发布";
headerRight_lableNext.visible = true;

//5秒防止多次点击
headerRight.on("touch", "", 5000, function() {
	circleAdd.submitAdd();
})
/****************处理页面-头部-end*****************/
var layout_main = ui("do_LinearLayout_main");
var addCategoryPicker = ui("ALayout_root").add("typeList","source://view/circle/base/manage/category.ui",0,0);
var addPicker = ui("ALayout_root").add("picker","source://view/reuse/form/oneCategory.ui", 0, 0);
var picker = ui("picker");
/**圈子内容**/
var addcircleRemark = layout_main.add("circleRemark", "source://view/reuse/form/mutiTextarea.ui",0,0);
var circleRemark = ui("circleRemark");
hashdata.addData({
    "textAreaHint":'圈子内容'
});
circleRemark.bindData(hashdata);
circleRemark.refreshData();

var addcircleTag = layout_main.add("circleTag", "source://view/reuse/form/mutiLabel.ui",0,0);
var circleTag = ui("circleTag");
circleTag.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "圈子话题",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
circleTag.refreshData();
/**圈子名称**/
var addcircleName = layout_main.add("circleName", "source://view/reuse/form/mutiInput.ui",0,0);
var circleName = ui("circleName");
circleName.bindData(hashdata);
hashdata.addData({
    "textTitle" : "圈子名称",
    "hint":'最多10个文字 '
});
circleName.refreshData();
/**圈子类别**/
var addtxtCategory = layout_main.add("txtCategory", "source://view/reuse/form/mutiLabel.ui",0,0);
var txtCategory = ui("txtCategory");
txtCategory.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "分类",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
txtCategory.refreshData();

ui("do_ScrollView_1").redraw();

var circleAdd = {
	categoryId: null,//分类id
	categoryName: null,
	circleTagId: null,
	circleTagName: null,
	openLevel:1,
	cityId:0,
	_category: [],
	_circleTag: [],
	init:function(){
		txtCategory.on("touch","",300,function(){//初始化圈子分类
			page.hideKeyboard();
			circleAdd.getCategory();
		});
		circleTag.on("touch","",300,function(){ //标签
			page.hideKeyboard();
			circleAdd.getCircleTag();
		});
		ui("do_ALayout_public").on("touch", function(data, e) { //公开性
			page.hideKeyboard();
			circleAdd.getPublic();
		});
		ui("do_ALayout_com").on("touch", "", 300, function() { //地区
			page.hideKeyboard();
			openHelper.open("source://view/area/cityMain.ui");
		});
	},
	getCategory:function(){
		if(circleAdd._category!=null && circleAdd._category.length > 0){
			page.fire("categoryList",circleAdd._category);
			page.fire("categorys","true");
		}else{
			httpSilence.post(circleUrl.url.category.levle1List, {}, function(data) {
				circleAdd._category = data.levle1List;
				page.fire("categoryList",data.levle1List);
				page.fire("categorys","true");
			})
		}
	},
	getCircleTag:function(){
		var ptitle = {"title" : "圈子话题","id" : "circleTagSelect"};
		if(circleAdd._circleTag!=null && circleAdd._circleTag.length > 0){
			page.fire("oneCategoryList", [ptitle,circleAdd._circleTag]);
			page.fire("oneCategory", "true");
		}else{
			httpSilence.post(communityUrl.url.message.tagList, {}, function(data) {
				circleAdd._circleTag = data.tagList;
				page.fire("oneCategoryList", [ptitle,data.tagList]);
				page.fire("oneCategory", "true");
			})
		}
	},
	getPublic:function(){
		var ptitle = {"title" : "请选择加圈方式","id" : "openLevelSelect"};
		var pcount = [ ptitle, [{"id":1,name:"完全公开(自由加入)"},{"id":2,name:"半开放(申请加入)"},{"id":3,name:"私密(受邀请加入)"}]];
		page.fire("oneCategoryList", pcount);
		page.fire("oneCategory", "true");
	},
	submitAdd:function(){
		page.hideKeyboard();
		var circleName = ui(addcircleName+".textInput").text;
		var remark = ui(addcircleRemark+".textArea").text;
		if(circleAdd.categoryId ==null){
			nf.toast("请选择分类");
			return;
		}
		if(circleName==""){
			nf.toast("请输入圈子名称!");
			return;
		}
		if(circleName.length>10){
			nf.toast("圈子名称长度限制为10个文字");
			return;
		}
		if(circleAdd.circleTagId ==null){
			nf.toast("请输入圈子话题");
			return;
		}
		if(remark==""){
			nf.toast("请输入圈子内容!");
			return;
		}
		var logoPath = imgLogo.source;
		if(logoPath=="" || logoPath ==null || logoPath.indexOf("source://")>-1){
			nf.toast("请添加图片");
			return;
		}
		var param = {
			"type": 2,//民间
			"categoryId": circleAdd.categoryId,
			"categoryName": circleAdd.categoryName,
			"name": circleName,
			"remark": remark,
			"tagName": circleAdd.circleTagName,
			"cityId": circleAdd.cityId,
			"openLevel": circleAdd.openLevel
		};
		http.upload(logoPath,"circle",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			if(string4imgs==""){
				nf.toast("请添加图片");
				return;
			}
			param.logo = string4imgs;
			deviceone.print(JSON.stringify(param));
			http.post(createCircleUrl,param,function(data){
				nf.toast("创建成功,管理员审核后才会在应用里显示");
				app.closePage({"circle_add":true});
			});
		});
	}
};
//点击背景隐藏输入键盘
root.on("touch",function(){
	page.hideKeyboard();
});
//二级回调
page.on("refreshCategory4Circle",function(data){
	var typeCategoryStr = ui(addtxtCategory+".labelText");
	if(data){
		if(data.sid != null){//如果有二级分类
			circleAdd.categoryId = data.sid;
			circleAdd.categoryName = data.sname;
			typeCategoryStr.text = data.bname + " / " + data.sname;
		}else if(data.bid != null){//如果只有一级分类
			circleAdd.categoryId = data.bid;
			circleAdd.categoryName = data.bname;
			typeCategoryStr.text = data.bname;
		}else{
			circleAdd.categoryId = null;
			circleAdd.categoryName = null;
			typeCategoryStr.text = '';
		}
	}else{
		circleAdd.categoryId = null;
		circleAdd.categoryName = null;
		typeCategoryStr.text = '';
	}
});
//选择标签或公开性--响应
page.on("refreshOneCategory", function(data) {
	if ("circleTagSelect" == data[1]) {//选择标签
		ui(addcircleTag+".labelText").text = data[0].onename;
		circleAdd.circleTagId = data[0].oneid;
		circleAdd.circleTagName = data[0].onename;
	}
	else if("openLevelSelect" == data[1]){//选择公开性
		circleAdd.openLevel = data[0].oneid;;
		ui("lablePublicName").text = data[0].onename;
	}
});
//从open的上层页面返回,刷新数据
//{cityName:childData.nameAll,cityId:childData.id}
page.on("result", function(data){
    if (data.cityName) {
    	circleAdd.cityId = data.cityId;
    	ui("lableCommName").text = data.cityName;
    }
});